package com.whfc.mach.queue;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.third.map.MapApi;
import com.whfc.common.third.map.MapLoc;
import com.whfc.common.util.Gps;
import com.whfc.common.util.JSONUtil;
import com.whfc.mach.dto.iot.MachMsg0x01;
import com.whfc.mach.dto.iot.MachWorkState;
import com.whfc.mach.entity.AppMachDataLog;
import com.whfc.mach.manager.MachDataManager;
import com.whfc.mach.manager.util.MachUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/12/2 11:59
 */
@Component
@RabbitListener(queues = {QueueConst.MACH_DATA, QueueConst.MACH_MSG_0X01})
public class MachDataQueueMessageListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MachDataManager machDataManager;

    @Autowired
    private MapApi mapApi;

    @RabbitHandler
    public void process(String msg) {
        try {
            logger.info("mach_data,{}", msg);
            MachMsg0x01 reqMsg = JSONUtil.parseObject(msg, MachMsg0x01.class);
            List<AppMachDataLog> logList = this.translate(reqMsg);
            machDataManager.addMachData(logList);

        } catch (Exception ex) {
            logger.error("mach_data,消息处理失败", ex);
        }
    }

    /**
     * 转换数据(排序+有效+去重)
     *
     * @param reqMsg
     * @return
     */
    private List<AppMachDataLog> translate(MachMsg0x01 reqMsg) {
//        Gps gcj02Gps = mapApi.translateToGcj02(reqMsg.getLngWgs84(), reqMsg.getLatWgs84());
//        MapLoc location = mapApi.geocode(gcj02Gps.getLng(), gcj02Gps.getLat());

        //数据排序
        List<MachWorkState> workStates = reqMsg.getWorkStates();
        Collections.sort(workStates, Comparator.comparing(o -> o.getTime()));

        //验证时间有效+去重
        List<AppMachDataLog> logList = new ArrayList<>(workStates.size());
        Date lastValidTime = null;
        Date serverTime = new Date();
        for (MachWorkState workState : workStates) {
            Date logTime = workState.getTime();
            if (MachUtil.isValidDataTime(logTime, serverTime)) {
                if (lastValidTime == null || !lastValidTime.equals(logTime)) {

                    AppMachDataLog log = new AppMachDataLog();
                    BeanUtils.copyProperties(reqMsg, log);
                    log.setDeviceId(reqMsg.getDeviceId());
                    log.setTime(workState.getTime());
                    log.setWorkState(workState.getWorkState());
//                    log.setLng(gcj02Gps.getLng());
//                    log.setLat(gcj02Gps.getLat());
//                    if (location != null) {
//                        log.setLocation(location.getAddress());
//                        log.setProvince(location.getProvince());
//                        log.setCity(location.getCity());
//                    }
                    log.setLng(reqMsg.getLngWgs84());
                    log.setLat(reqMsg.getLatWgs84());

                    logList.add(log);
                    lastValidTime = logTime;
                }
            }
        }
        return logList;
    }
}
